package com.danceforyou.user.service.impl;

import com.danceforyou.common.restful.exception.BizExceptionEnum;
import com.danceforyou.common.service.OperationService;
import com.danceforyou.common.service.impl.BaseServiceImpl;
import com.danceforyou.common.util.AssertUtil;
import com.danceforyou.common.util.Constants;
import com.danceforyou.user.dao.RoleDAO;
import com.danceforyou.user.model.RoleDO;
import com.danceforyou.user.model.UserDO;
import com.danceforyou.user.service.RoleMenuService;
import com.danceforyou.user.service.RoleService;
import com.danceforyou.user.service.UserService;
import com.danceforyou.user.vo.RoleVO;
import com.danceforyou.user.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
* 类名称：RoleServiceImpl
* 创建人：danceforyou
* 创建时间：2021年01月10日
* 版本号：1.0v
*/
@Service("roleService")
public class RoleServiceImpl extends BaseServiceImpl<Long, RoleDO, RoleVO, RoleDAO> implements RoleService {
    @Autowired
    private RoleDAO roleDAO;
    @Autowired
    private OperationService operationService;
    @Autowired
    private UserService userService;
    @Autowired
    private RoleMenuService roleMenuService;
    @Autowired(required = false)
    private HttpSession session;

    @Override
    public RoleDAO getDao() {
        return roleDAO;
    }

    @Override
    public void saveInfo(RoleDO role) {
        UserVO loginUser = (UserVO) session.getAttribute(Constants.SESSION_USER);
        AssertUtil.notNull(loginUser, BizExceptionEnum.AUTHFAILURE);
        //获取上级岗位/部门
        RoleDO parentRole = roleDAO.getById(role.getPId());
        AssertUtil.notNull(parentRole, "上级岗位/部门信息不存在");
        //保存岗位信息
        role.setType(1);//类型（0-部门，1-岗位）
        roleDAO.insert(role);
        //记录操作日志
        operationService.saveOperation(Constants.OPER_TABLE_ROLE, role.getId(), Constants.OPER_ADD, loginUser, null);
    }

    @Override
    public void updateInfo(RoleDO role) {
        UserVO loginUser = (UserVO) session.getAttribute(Constants.SESSION_USER);
        AssertUtil.notNull(loginUser, BizExceptionEnum.AUTHFAILURE);
        RoleDO temp = roleDAO.getById(role.getId());
        AssertUtil.notNull(temp, "信息不存在");
        roleDAO.update(role);
        //记录操作日志
        operationService.saveOperation(Constants.OPER_TABLE_ROLE, role.getId(), Constants.OPER_EDIT, loginUser, null);
    }

    @Override
    public int deleteInfoByIds(String ids) {
        UserVO loginUser = (UserVO) session.getAttribute(Constants.SESSION_USER);
        AssertUtil.notNull(loginUser, BizExceptionEnum.AUTHFAILURE);
        String[] idArr = ids.split(",");
        int i = 0;
        for (String id : idArr) {
            RoleDO role = roleDAO.getById(Long.valueOf(id));
            AssertUtil.notNull(role, "信息不存在");
            //判断该岗位是否已有用户
            List<UserDO> userList = userService.listByProperty("roleId", role.getId());
            AssertUtil.isTrue(userList.isEmpty(), "该岗位已有员工，无法删除");
            i += roleDAO.deleteById(role.getId());
            //删除岗位对应菜单
            roleMenuService.deleteByProperty("roleId", role.getId());
            //记录操作日志
            operationService.saveOperation(Constants.OPER_TABLE_ROLE, role.getId(), Constants.OPER_DELETE, loginUser, null);
        }
        return i;
    }
}
